package com.pw.study.arithmetic.stack

/**
 * @Desc: 栈位
 * @author: future
 * @since: 2022/5/30-20:16
 */
class FutureStack(num: Int = 50) {


  /**
   * 栈空
   *
   * @return
   */
  def isEmpty(): Boolean = {
    top == -1
  }

  /**
   * 栈满
   *
   * @return
   */
  def isFull(): Boolean = {
    top == maxSize - 1
  }

  val maxSize = num
  private var stack: Array[Int] = new Array[Int](maxSize)
  var top: Int = -1

  /**
   * 出栈
   */
  def pop(): Any = {
    if (isEmpty()) {
      return new Exception("栈空。。。")
    }
    val value = stack(top)
    top -= 1
    value
  }

  /**
   * 压入数据
   *
   * @param value
   */
  def push(value: Int): Unit = {
    if (isFull()) {
      println("栈满。。")
      return
    }
    top += 1
    stack(top) = value
  }

  /**
   * 栈位遍历
   */
  def list(): Unit = {
    if (isEmpty()) {
      println("栈空，没有数据。。。")
      return
    }
    for (i <- 0 to top reverse) {
      println(s"stack[$i] = ${stack(i)}")
    }
  }


}
